let app = getApp();

Component({

  /**
   * 组件的属性列表
   */
  properties: {
    homeIsPulldown: {
      type: Boolean,
      value: false,
      observer: "onPullDownMethod"
    },

    onAudioBottomCount: {
      type: Number,
      value: 1,
      observer: 'onAudioBottomMethod'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    activeCategor: -1,
    category: [],
    categoryList: [],
    isLoading: true, // 是否加载完成
    pageNo: 1,
    pageSize: 30,
    totalPage: 1,
    status: 1,
    recommend: '',
    is_free: '',
    video_source: '',
    isLanguageTypeValue: 1,
    keyWord: '',
    categoryId: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 获取首页内容
    getPost() {
      let that = this;
      wx.request({
        url: `${app.globalData.API_URL}/kitap`,
        data: {
          pageNo: this.data.pageNo,
          pageSize: this.data.pageSize,
          status: this.data.status,
          categoryId: this.data.activeCategor,
          recommend: this.data.recommend,
          is_free: this.data.is_free,
          video_source: this.data.video_source,
          isLanguageTypeValue: this.data.isLanguageTypeValue,
          keyWord: this.data.keyWord,
        },
        header: {
          'content-type': 'application/json'
        },
        success (res) {
          if(res.statusCode == 200) {
            let categoryList = res.data.data
            that.setData({ categoryList: that.data.categoryList.concat(categoryList), totalPage: res.data.totalPage, isPageLoading: false })
            that.LoadingFinish(); // // 完成加载
          }
        }
      })
    },

    // 点击分类
    onClickCategory(e) {
      let cotegoryId = parseInt(e.currentTarget.dataset.item.id, 10);
      this.setData({ activeCategor: cotegoryId, categoryList: [] });
      this.getPost();
    },

    // 点击书
    onClickBook(item) {
      let postId = item.currentTarget.dataset.item.id;
      wx.navigateTo({
        url: `/pages/audioList/audioList?postId=`+postId,
      })
    },

    // 检查用户登录
    isLogin() {
      let userInfo = wx.getStorageSync('userInfo')
      if (!userInfo) {
        app.login( res => {
          this.setData({ userInfo: res.user })
        })
      } else {
        this.setData({ userInfo: userInfo.user })
      }
    },

    // 获取类目
    getCategory() {
      let that = this;
      wx.request({
        url: `${app.globalData.API_URL}/category`,
        data: {
          pageNo: this.data.pageNo,
          pageSize: 100,
        },
        header: {
          'content-type': 'application/json'
        },
        success (res) {
          if(res.statusCode == 200) {
            res.data.unshift({ id: -1, name: 'ھەممىسى' })
            that.setData({ category: res.data })
          }
        }
      })
    },

    // 分页
    noMoreMethod(pull) {
      if(this.data.pageNo == this.data.totalPage) return
      let pageNo = this.data.pageNo += 1
      this.setData({pageNo : pageNo, isPageLoading: true})
      this.getPost();
    },

    // 获取高实现翻页
    onAudioBottomMethod(number) {
      // 当lessons滚到页面底部时触发
      this.noMoreMethod(false)
    },

    // 加载完成
    LoadingFinish() {
      setTimeout(() => {
        this.setData({ isLoading: false })
      }, 300)
    },
  },

  lifetimes: {
    attached: function() {
      this.isLogin();
      this.getPost();
      this.getCategory();
    },

  }
})